﻿using CJAtech.BBS.Business;
using CJAtech.BBS.DataEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TjZheng.Framework;

namespace TjZheng.CMS.Process.Business
{
    public partial class CategoryInfoProcess
    {
        public string ConnectionStringKey { get; set; }
        public string DataObjectParam { get; set; }
        public CategoryInfoProcess(string ConnectionStringKey = "", string DataObjectParam = "")
        {
            this.ConnectionStringKey = ConnectionStringKey;
            this.DataObjectParam = DataObjectParam;
        }
        public TreeEntity BindTree(int id)
        {
            BizCategoryInfo objBizCategoryInfo = new BizCategoryInfo(this.ConnectionStringKey, this.DataObjectParam);
            CategoryInfo objCategoryInfo = objBizCategoryInfo.GetRecord(id);
            if (objCategoryInfo == null)
            {
                return null;
            }
            var objCategoryInfoList = objBizCategoryInfo.GetList(string.Format("IDPath like '{0}%'", objCategoryInfo.IDPath));
            return objCategoryInfoList.ConvertToTree(objCategoryInfo.CategoryId);
        }
        public List<TreeEntity> BindTrees(int id)
        {
            BizCategoryInfo objBizCategoryInfo = new BizCategoryInfo(this.ConnectionStringKey, this.DataObjectParam);
            CategoryInfo objCategoryInfo = objBizCategoryInfo.GetRecord(id);
            if (objCategoryInfo == null)
            {
                return new List<TreeEntity>();
            }
            var objCategoryInfoList = objBizCategoryInfo.GetList(string.Format("IDPath like '{0}%'", objCategoryInfo.IDPath));
            return objCategoryInfoList.ConvertToTreeList(objCategoryInfo.CategoryId) ?? new List<TreeEntity> { };
        }
        public List<TreeEntity> BindTreesWithOther(int id)
        {
            var objTreeEntityList = this.BindTrees(id);
            if (objTreeEntityList.IsNoNull())
            {
                var objRoot = objTreeEntityList.First(c => c.id == id);
                var objTreeEntity = new TreeEntity();
                objTreeEntity.id = 0;
                objTreeEntity.text = "其他日志";
                objTreeEntity.iconCls = "icon-application";
                objRoot.children.Add(objTreeEntity);
            }
            return objTreeEntityList;
        }
        public List<TreeEntity> BindTreeList(int id)
        {
            BizCategoryInfo objBizCategoryInfo = new BizCategoryInfo(this.ConnectionStringKey, this.DataObjectParam);
            CategoryInfo objCategoryInfo = objBizCategoryInfo.GetRecord(id);
            if (objCategoryInfo == null)
            {
                return new List<TreeEntity>();
            }
            var objCategoryInfoList = objBizCategoryInfo.GetList(string.Format("IDPath like '{0}%'", objCategoryInfo.IDPath));
            objCategoryInfoList = objCategoryInfoList.Except(new List<CategoryInfo> { objCategoryInfo }).ToList();
            return objCategoryInfoList.ConvertToTreeList(objCategoryInfo.CategoryId) ?? new List<TreeEntity> { };
        }
    }
}
